<?php

namespace App\Http\Controllers\Web;

use App\Http\Controllers\Controller;
use App\Models\ShopRoomTable;
use Illuminate\Http\Request;
use App\Repositories\RoomTableRepository;
use Illuminate\Support\Facades\Validator;

Class RoomTableController extends Controller
{
    protected $roomTableRepository;

    public function __construct(RoomTableRepository $roomTableRepository)
    {
        $this->roomTableRepository=$roomTableRepository;
    }

    //新建/修改房桌
    public function createRoomTable(Request $request)
    {
        $rules = [
            'shop_id' => 'required|integer|exists:shops,id',
            'type' => 'required|in:0,1',
            'hold'=>'required|integer',
            'name'=>'required|string',
            'min_consumption'=>'required|numeric|min:0',
            'lock_qrcode'=>'required|integer|in:0,1',
            'thumb'=>'required|string',
        ];
        $message = [
            'shop_id.*'=>'商家信息有误',
            'type.*'=>'请选择正确的方桌类型，记得勾选',     //0桌子1房间
            'hold.*'=>'请认真填写正确的容纳人数',
            'name.*'=>'请认真填写房间/桌子名称',
            'min_consumption.*'=>'请认真填写最低消费',
            'lock_qrcode.*'=>'请选择是否启用二维码',      // 是否启用二维码(0否，1是)
            'thumb.*'=>'请上传图片'
        ];
        $validator = Validator::make($request->all(), $rules,$message);
        if ($validator->fails()) {
            return show(202,$validator->errors()->first());
        }
        return $this->roomTableRepository->createRoomTable($request);
    }
    public function updateRoomTable(Request $request)
    {
        $rules = [
            'id' => 'required|integer|exists:shop_room_tables,id',
            'shop_id' => 'required|integer|exists:shops,id',
            'type' => 'required|in:0,1',
            'hold'=>'required|integer',
            'name'=>'required|string',
            'min_consumption'=>'required|numeric|min:0',
            'lock_qrcode'=>'required|integer|in:0,1',
            'thumb'=>'required|string',
        ];
        $message = [
            'id.*'=>'请选择正确的房桌',
            'shop_id.*'=>'商家信息有误',
            'type.*'=>'请选择正确的方桌类型，记得勾选',     //0桌子1房间
            'hold.*'=>'请认真填写正确的容纳人数',
            'name.*'=>'请认真填写房间/桌子名称',
            'min_consumption.*'=>'请认真填写最低消费',
            'lock_qrcode.*'=>'请选择是否启用二维码',      // 是否启用二维码(0否，1是)
            'thumb.*'=>'请上传图片'
        ];
        $validator = Validator::make($request->all(), $rules,$message);
        if ($validator->fails()) {
            return show(202,$validator->errors()->first());
        }
        return $this->roomTableRepository->updateRoomTable($request);
    }

    //删除房桌
    public function deleteRoomTable(Request $request)
    {
        $rules = [
            'id' => 'required|integer|exists:shop_room_tables,id',
        ];
        $message = [
            'id.*'=>'房桌信息有误'
        ];
        $validator = Validator::make($request->all(), $rules,$message);
        if ($validator->fails()) {
            return show(202,$validator->errors()->first());
        }
        $id = $request->get('id');
        return $this->roomTableRepository->deleteRoomTable($id);
    }

    //房桌状态
    public function showRoomTable(Request $request)
    {
        $rules = [
            'shop_id' => 'required|integer|exists:shops,id',
        ];
        $message = [
            'shop_id.*'=>'商家信息有误'
        ];
        $validator = Validator::make($request->all(), $rules,$message);
        if ($validator->fails()) {
            return show(202,$validator->errors()->first());
        }
        $shop_id = $request->get('shop_id');
        return $this->roomTableRepository->showRoomTables($shop_id);
    }

    public function showOneRoomTable(Request $request)
    {
        $rules = [
            'id' => 'required|integer|exists:shop_room_tables,id',
        ];
        $message = [
            'id.*'=>'请选择正确餐桌'
        ];
        $validator = Validator::make($request->all(), $rules,$message);
        if ($validator->fails()) {
            return show(202,$validator->errors()->first());
        }
        $id = $request->id;
        return $this->roomTableRepository->showOne($id);
    }

}
